package com.atguigu.dao.Impl;

import com.atguigu.dao.BaseDao;
import com.atguigu.dao.BookDao;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.User;

import java.util.List;

/**
 * @ClassName BookDaoImpl
 * @Description TODO
 * @Author 周轩霆
 * @Date 2022/4/16 14:49
 **/

public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book(`name` , `author` , `price` , `sales` , `stock` , `img_path`) " +
                "values(?,?,?,?,?,?)";
        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id= ?";
        return update(sql, id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update  t_book set `name`=? , `author`=? , `price`=? , `sales`=? , `stock`=? , `img_path`=?  where id= ?";

        return update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath(), book.getId());


    }

    @Override
    public Book queryBookById(Integer id) {
        //加别名.pojo中变量名与数据库中不一致
        String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book where id= ?";
        return queryForOne(Book.class, sql, id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book";

        return queryForList(Book.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        //求总的记录数
        String sql = "select count(*) from t_book ";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }


    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) {
        //查询当前页的数据
        String sql="select id,name,author,price,sales,stock,img_path imgPath from t_book limit ?,? ";
        return queryForList(Book.class,sql,begin,pageSize);


    }

    @Override
    public Integer queryForPageTotalByPrice(int min, int max) {
        //求一定价格区间内总的记录数
        String sql = "select count(*) from t_book where price between ? and ? ";
        Number count = (Number) queryForSingleValue(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Book> queryForPageByPrice(int begin, int pageSize, int min, int max) {
        String sql="select * from t_book where price between ? and ? order by price  limit ?,? ";
        return queryForList(Book.class,sql,min,max,begin,pageSize);
    }


}


